package adv8;

import java.io.File;
import java.io.FileWriter;
import java.util.List;
import java.util.Scanner;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class ModifyProduct {

	public static void main(String[] args) throws Exception {
		Scanner in = new Scanner(System.in);
		System.out.println("输入商品号：");
		String no = in.next();
		System.out.println("新数量：");
		int num = in.nextInt();
		SAXReader reader = new SAXReader();
		Document doc = reader.read(new File("products.xml"));
		Element e = (Element) doc.selectSingleNode("/shop/product[itemno=" + no + "]");
		if (e == null) {
			System.out.println("没有此商品");
			return;
		}
		e.selectSingleNode("stockcount").setText(num + "");
//		boolean isIn = false;
//		for (Element e : list) {
//			// 找到e的子节点itemno
//			String n = e.selectSingleNode("itemno").getText();
//			if (n.equals(no)) {
//				isIn = true;
//				e.selectSingleNode("stockcount").setText(num + "");
//			}
//		}
//		if (!isIn) {
//			System.out.println("没有此商品");
//			return;
//		}
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("utf-8");
		XMLWriter xm = new XMLWriter(new FileWriter("products.xml"), format);
		xm.write(doc);
		xm.close();// 关闭
		System.out.println("修改成功");
	}

}
